﻿using AlipayPay.Infrastructure.Config;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;

namespace AlipayPay.Infrastructure
{
    /// <summary>
    /// 配置文件信息
    /// </summary>
    public class Appsettings
    {
        static IConfiguration Configuration { get; set; }

        public Appsettings(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        #region 封装要操作的字符

        /// <summary>
        /// 封装要操作的字符
        /// </summary>
        /// <param name="sections">节点配置</param>
        /// <returns></returns>
        public static string App(params string[] sections)
        {
            try
            {
                if (sections.Any())
                {
                    return Configuration[string.Join(":", sections)];
                }
            }
            catch (Exception) { }

            return "";
        }

        #endregion

        #region 递归获取配置信息数组

        /// <summary>
        /// 递归获取配置信息数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sections"></param>
        /// <returns></returns>
        public static List<T> App<T>(params string[] sections)
        {
            List<T> list = new List<T>();
            Configuration.Bind(string.Join(":", sections), list);
            return list;
        }

        #endregion

        #region 获取AppConfig配置信息

        /// <summary>
        /// 获取AppConfig配置信息
        /// </summary>
        /// <returns></returns>
        public static AppConfig GetAppConfig()
        {
            AppConfig appConfig = new AppConfig();

            appConfig.DbConn = App(new string[] { "AppConfig", "DbConn" });
            appConfig.RunTask = Convert.ToBoolean(App(new string[] { "AppConfig", "RunTask" }));
            appConfig.AesKey = App(new string[] { "AppConfig", "AesKey" });
            return appConfig;
        }

        #endregion
    }
}
